package com.b2c.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.b2c.product.mapper.SpecParamMapper;
import com.b2c.product.pojo.dto.SpecParamDto;
import com.b2c.product.pojo.entity.SpecParam;
import com.b2c.product.pojo.vo.SpecParamVo;
import com.b2c.product.service.SpecParamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import result.Result;

import java.util.ArrayList;
import java.util.List;

import static content.common.ResultMessage.*;

/**
 * @author Administrator
 * @description 针对表【mall_spec_param(参数表)】的数据库操作Service实现
 * @createDate 2024-03-20 17:29:59
 */
@Service
@Slf4j
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam>
        implements SpecParamService {
    /**
     * 通过品类编号获取参数
     *
     * @param spgId
     * @return
     */
    @Override
    public Result getBySpgId(Integer spgId) {
        log.info("通过品类编号获取参数:{}",spgId);
        QueryWrapper<SpecParam> query = new QueryWrapper<SpecParam>().eq("spg_id", spgId);
        List<SpecParam> specParams = list(query);
        if (specParams.isEmpty()) {
            return Result.fail(NO_PARAM);
        }
        return Result.success(specParams);
    }

    /**
     * 添加参数
     *
     * @param specParamDto
     * @return
     */
    @Override
    public Result insert(SpecParamDto specParamDto) {
        log.info("添加参数{}",specParamDto);
        SpecParam specParam = BeanUtil.copyProperties(specParamDto, SpecParam.class);
        boolean save = save(specParam);
        if (!save) {
            return Result.fail(ISNERT_ERROR);
        }
        return Result.success();
    }

    /**
     * 删除参数
     *
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        log.info("删除参数:{}",id);
        boolean b = removeById(id);
        if (!b) {
            return Result.fail(DELETE_ERROR);
        }
        return Result.success();
    }

    /**
     * 更改品类参数
     *
     * @param specParamDto
     * @return
     */
    @Override
    public Result updateSpecParam(SpecParamDto specParamDto, Integer id) {
        log.info("更改品类参数");
        SpecParam specParam = BeanUtil.copyProperties(specParamDto, SpecParam.class);
        specParam.setId(id);
        boolean updateById = updateById(specParam);
        if (!updateById){
            return Result.fail(UPDATE_ERROR);
        }
        return Result.success();
    }

    /**
     * 通过参数编号获取参数
     * @param sppId
     * @return
     */
    @Override
    public Result getBySppId(Integer sppId) {
        log.info("获取参数,编号:{}",sppId);
        QueryWrapper<SpecParam> specParamQueryWrapper = new QueryWrapper<SpecParam>().eq("spp_id",sppId);
        SpecParam specParam = getOne(specParamQueryWrapper);
        if (specParam==null){
            return Result.fail(NO_PARAM);
        }
        return Result.success(specParam);
    }

    /**
     * 商家通过spgId获取参数
     * @param branchId
     * @return
     */
    @Override
    public Result getBranchById(Integer branchId) {
        log.info("通过品类编号获取参数:{}",branchId);
        //查询数据库
        QueryWrapper<SpecParam> query = new QueryWrapper<SpecParam>().eq("spg_id", branchId);
        List<SpecParam> specParams = list(query);
        if (specParams.isEmpty()) {
            return Result.fail(NO_PARAM);
        }
        //转换成vo对象
        ArrayList<SpecParamVo> specParamVos = new ArrayList<>();
        for (SpecParam specParam : specParams) {
            SpecParamVo specParamVo = new SpecParamVo();
            BeanUtils.copyProperties(specParam,specParamVo);
            specParamVos.add(specParamVo);
        }
        return Result.success(specParamVos);
    }


}




